package com.rovinj_server.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rovinj_server.R;

import java.net.ServerSocket;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TextActivity extends Activity {

    public static ServerSocket serverSocket = null;
    @Bind(R.id.ed_ke)
    EditText mEdKe;
    @Bind(R.id.butt_she)
    Button mButtShe;
    @Bind(R.id.textView1)
    TextView mTextView1;

    private SharedPreferences mSp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        ButterKnife.bind(this);
        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String DEVICE_ID = tm.getDeviceId();
        initId();
    }

    private void initId() {
        String getlocalip = getlocalip();
        mTextView1.setText(getlocalip);
        mSp = getSharedPreferences("FWDID", Context.MODE_PRIVATE);
        if (mSp != null) {
            String id = mSp.getString("id", "");
            mEdKe.setText(id + "");
        }

        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String DEVICE_ID = tm.getDeviceId();
        mTextView1.setText(DEVICE_ID);
    }


    private String getlocalip() {   //获取ID
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int i = wifiInfo.getIpAddress();
        System.out.println("int ip " + i);
        if (i == 0) return null;
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);

    }

    @OnClick(R.id.butt_she)
    public void onClick() {
        String id = mEdKe.getText().toString();
        SharedPreferences.Editor edit = mSp.edit();
        edit.putString("id", id);
        edit.commit();

        String edID = mSp.getString("id", "");

        if (id.equals(edID)) {
            Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "请重新设置", Toast.LENGTH_SHORT).show();
        }
    }
}